/*
 * @Date: 2022-02-11 16:39:56
 * @LastEditors: tich425
 * @LastEditTime: 2022-02-11 16:47:42
 * @FilePath: \TodoList\utils\captcha.go
 * @Description: 生成验证码
 */
package utils

import (
	"log"
	"time"
	"todo-list/global"
)

/**
 * @description: 生成验证码
 * @param {string} email
 */
func GenerateCode(email string) bool {
	// 生成验证码
	code := RandomNumber(6)
	// 存储至 Redis
	if err := global.REDIS_DB.Set(email, code, time.Minute*time.Duration(global.CONFIG.Email.Expires)).Err(); err != nil {
		log.Print("Redis set error: ", err)
		return false
	}
	return SendEmail(email, code)
}

/**
 * @description: 验证验证码
 * @param {string} email
 * @param {string} code
 */
func VerifyCode(email string, code string) bool {
	// 获取 Redis 中的验证码
	redisCode, err := global.REDIS_DB.Get(email).Result()
	if err != nil {
		return false
	}
	// 比较验证码
	if code != redisCode {
		return false
	}
	return true
}
